<?php

namespace App\Jobs;

use App\Models\FileModel;
use App\Models\IndexShidao;
use App\Services\ElasticSearchService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;


class FileJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $handType; // A:新增，D:删除，R：重命名，M：修改
    private $rootPath;
    private $realPath;
    private $fullPath;
    private $content;
    private $virtualPath;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($handType, $rootPath, $realPath, $virtualPath, $note)
    {
        $this->handType = $handType;
        $this->rootPath = $rootPath;
        $this->realPath = $realPath;
        $this->fullPath = $rootPath . $realPath;
        $this->content  = $note;
        $this->virtualPath = $virtualPath;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $handType = $this->handType;
        $searchService  = new ElasticSearchService();
        $indexShidao    = new IndexShidao();
        if ($handType == 'D') {
            /**
             * 删除
             * 根据 路径删除内容 为了便于迁移 路径只存相对目录
             */
            $fileInfo   = FileModel::find_by_path($this->realPath);
            if ($fileInfo) {
                $searchService->delete($fileInfo->id);
                $fileInfo->delete();
            }

        } elseif ($handType == 'M' || $handType == 'R') {
            /**
             * 修改
             * 检查是否已经有相关记录
             * 修改-根据ID直接修改
             * 命令-根据内容重新命名
             */
            $fileInfo = null;
            if($this->type == 'M') {    // 修改内容

                $fileInfo = FileModel::find_by_path($this->realPath);
                if ($fileInfo) {
                    $content = $this->get_content();
                    $fileInfo->content = $content;
                    $fileInfo->real_path = $this->realPath;

                    $indexShidao->content = $content;
                    $indexShidao->filepath = $this->realPath;
                }

            } elseif($handType == 'R') {  // 重命名

                $hash = $this->get_hash();
                $fileInfo = FileModel::find_by_hash($hash);
                if ($fileInfo) {

                    $fileInfo->virtual_path = $this->virtualPath;
                    $indexShidao->title = $this->virtualPath;
                }
            }

            if ($fileInfo) {
                $fileInfo->save();
                $searchService->update($fileInfo->id, $indexShidao->data());
            }

        } else {
            /**
             * 新增
             */
            // $this->get_content();
            $content    = $this->content;
            $hash       = $this->get_hash();
            $this->create_thumb();
            $fileInfo   = FileModel::create(['path' => $this->realPath, 'virtual_path'=> $this->virtualPath, 'hash' => $hash]);
            $indexShidao->title     = $this->virtualPath;
            $indexShidao->content   = $content;
            $indexShidao->filepath  = $this->realPath;
            $searchService->add($fileInfo->id, $indexShidao->data());
        }
    }

    /**
     * 内容识别，通过机器学习获取到文件内容
     * @return string
     */
    private function get_content() {

        $content = "";
        return $content;
    }

    private function get_hash() {

        return md5_file($this->fullPath);
    }

    /**
     * 创建缩略图
     */
    function create_thumb() {
        $ext    = pathinfo($this->fullPath, PATHINFO_EXTENSION);
        $name   = pathinfo($this->fullPath, PATHINFO_FILENAME);
        $dir    = dirname($this->fullPath);

        if (in_array($ext, ['jpg', 'jpeg', 'png'])) {

            resizeImage($this->fullPath, "{$dir}/{$name}-200.{$ext}", 200);
        }
    }
}
